package cn.tedu.pet.product.controller;

import cn.tedu.pet.commons.pojo.vo.PageData;
import cn.tedu.pet.product.pojo.param.BreedAddNewParam;
import cn.tedu.pet.product.pojo.param.BreedUpdateParam;
import cn.tedu.pet.product.pojo.vo.BreedListItemVO;
import cn.tedu.pet.product.pojo.vo.BreedStandardVO;
import cn.tedu.pet.product.pojo.vo.BreedTreeItemVO;
import cn.tedu.pet.product.service.IBreedService;
import cn.tedu.pet.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/breed")
@Api(tags = "02.品种管理模块")
@Slf4j
@Validated
public class BreedController {

    @Autowired
    private IBreedService breedService;


    @PostMapping("/add-new")
    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid BreedAddNewParam breedAddNewParam) {
        log.debug("开始处理【添加类别】的请求，参数：{}", breedAddNewParam);
        breedService.addNew(breedAddNewParam);
        return JsonResult.ok();
    }


    @GetMapping("/list")
    @ApiOperation("根据父级查询子级列表")
    @ApiOperationSupport(order = 421)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父级类别ID", paramType = "query")
    })
    public JsonResult BreedListItemVO (@Range(message = "请提交有效的父级类别ID值！") Long parentId) {
        log.debug("开始处理【根据父级类别查询子级类别列表】", parentId);
        List<BreedListItemVO> list = breedService.listByParentId(parentId);
        return JsonResult.ok(list);
    }

    @GetMapping("/listall")
    @ApiOperation("查询类别列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult list(@Range(min = 1, message = "查询类别列表失败，请提供正确的页码值！") Integer page) {
        log.debug("开始处理[查询类别列表]的请求，页码：{}", page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<BreedListItemVO> pageData = breedService.list(page);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/standard")
    @ApiOperation("根据ID查询类别详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult  getStandardById(
            @RequestParam @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【根据ID查询类别详情】的请求，参数：{}", id);
        BreedStandardVO queryResult = breedService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("/tree")
    @ApiOperation("查询类别树")
    @ApiOperationSupport(order = 420)
    public JsonResult  CategoryTreeItemVO () {
        log.debug("开始处理【获取类别树】的业务，参数：无");
        List<BreedTreeItemVO> categoryTree = breedService.listTree();
        return JsonResult.ok(categoryTree);
    }

    @PostMapping("/delete")
    @ApiOperation("删除类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "类别ID",required = true,dataType = "long")
    })
    public JsonResult delete(@Range(min = 1,message = "根据ID删除类别失败,请提交合法的ID值!")
                             @RequestParam Long id){
        log.debug("处理[删除类别数据]的请求,参数:{}",id);
        breedService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改类别数据")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "宠物ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@RequestParam @Range(min = 1,message = "请提交有效的类别id值!") Long id,
                                     @Valid BreedUpdateParam breedUpdateParam) {
        log.debug("开始处理[修改类别详情]的业务，参数：{}", breedUpdateParam);
        breedService.updateInfoById(id,breedUpdateParam);
        return JsonResult.ok();
    }

}
